package com.jotta.client.data;

import java.util.ArrayList;

import com.jotta.client.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemAdapter extends ArrayAdapter<ItemInfo> {

	private ArrayList<ItemInfo> items;

	public ItemAdapter(Context context, int textViewResourceId,
			ArrayList<ItemInfo> items) {
		super(context, textViewResourceId, items);
		this.items = items;
	}

	/**
	 * Update list of item info, current list will be cleared before update
	 * 
	 * @param list: List of Item info
	 */
	public void updateItemInfoList(ArrayList<ItemInfo> list) {
		this.clear();
		for (int index = 0; index < list.size(); index++) {
			this.add(list.get(index));
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.row, null);
		}
		ItemInfo itemInfo = items.get(position);
		if (itemInfo != null) {
			TextView lbltitle = (TextView) v.findViewById(R.id.row_title);
			TextView lbldescription = (TextView) v
					.findViewById(R.id.row_description);
			ImageView imgIcon = (ImageView) v.findViewById(R.id.row_icon);
			if (lbltitle != null) {
				lbltitle.setText(itemInfo.getTitle());
			}
			if (lbldescription != null) {
				lbldescription.setText(itemInfo.getDescription());
			}
			if (imgIcon != null) {
				imgIcon.setImageResource(itemInfo.getImageResourceId());
			}
		}
		return v;
	}
}
